package com.zone.customview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by zone on 2017/4/30.
 */

public class HuaWeiColockView extends View {

    private Paint mPaint;
    private Paint redPointPaint;
    private Paint mTextPaint;
    private float mGradee;
    private int second = 0;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                second++;
                postInvalidate();
                mHandler.sendEmptyMessageDelayed(0, 1000);

            }
        }
    };

    public HuaWeiColockView(Context context) {
        super(context);
        init();

    }

    public HuaWeiColockView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();

    }

    public HuaWeiColockView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public HuaWeiColockView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeWidth(30);

        redPointPaint = new Paint();
        redPointPaint.setColor(getResources().getColor(R.color.huaweiClockRed));
        redPointPaint.setAntiAlias(true);
        redPointPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        redPointPaint.setStrokeWidth(30);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mTextPaint.setStrokeWidth(5);
        mTextPaint.setTextSize(150);

        mGradee = 360 / 60 / 2;

//        Thread thread = new Thread(new Runnable() {
//            @Override
//            public void run() {
        mHandler.sendEmptyMessageDelayed(0, 1000);


//            }
//        });
//        thread.start();


    }

    private int getTextHeight(String text) {
        Paint.FontMetrics fm = mTextPaint.getFontMetrics();
        return (int) Math.ceil(fm.descent - fm.ascent);
    }

    @Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(getResources().getColor(R.color.huaweiClockView));

        canvas.translate(getWidth() / 2, getHeight() / 2);
        canvas.drawText("00:36", 0 - mTextPaint.measureText("00:36") / 2, 0 + getTextHeight("00:36") / 2, mTextPaint);


        Log.d("zoneLog", "onDraw: " + mGradee);
        for (int i = 0; i < 120; i++) {
//            if (i % 5 == 0) {
//                mPaint.setStrokeWidth(20);
//            } else {
            mPaint.setStrokeWidth(8);
//            }
            canvas.drawLine(0, 400, 0, 500, mPaint);
            canvas.rotate(mGradee);
            canvas.save();
        }


        ValueAnimator value = ValueAnimator.ofFloat(0, mGradee);
        value.setDuration(1000);
        value.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
//                mGradee = (float) animation.getAnimatedValue();
                canvas.rotate(mGradee * (second - 1) + (float) animation.getAnimatedValue());
                canvas.drawCircle(0, 350, 20, mPaint);//绘制红色圆点


                canvas.drawLine(0, 400, 0, 520, mPaint);//
                canvas.rotate(-mGradee * 3);
                canvas.drawLine(0, 400, 0, 506, mPaint);//
                canvas.rotate(mGradee);
                canvas.drawLine(0, 400, 0, 512, mPaint);//
                canvas.rotate(mGradee);
                canvas.drawLine(0, 400, 0, 518, mPaint);//


                canvas.rotate(mGradee * 2);
                canvas.drawLine(0, 400, 0, 518, mPaint);//
                canvas.rotate(mGradee);
                canvas.drawLine(0, 400, 0, 512, mPaint);
                canvas.rotate(mGradee);
                canvas.drawLine(0, 400, 0, 506, mPaint);//
            }
        });
//        value.start();

        canvas.rotate(mGradee * second);
        canvas.drawCircle(0, 350, 20, redPointPaint);//绘制红色圆点


        canvas.drawLine(0, 400, 0, 520, mPaint);//
        canvas.rotate(-mGradee * 3);
        canvas.drawLine(0, 400, 0, 506, mPaint);//
        canvas.rotate(mGradee);
        canvas.drawLine(0, 400, 0, 512, mPaint);//
        canvas.rotate(mGradee);
        canvas.drawLine(0, 400, 0, 518, mPaint);//


        canvas.rotate(mGradee * 2);
        canvas.drawLine(0, 400, 0, 518, mPaint);//
        canvas.rotate(mGradee);
        canvas.drawLine(0, 400, 0, 512, mPaint);
        canvas.rotate(mGradee);
        canvas.drawLine(0, 400, 0, 506, mPaint);//


//        canvas.rotate(-mGradee * 3);
//        canvas.drawLine(0, 400, 0, 506, mPaint);//
//        canvas.rotate(mGradee);
//        canvas.drawLine(0, 400, 0, 512, mPaint);//
//        canvas.rotate(mGradee);
//        canvas.drawLine(0, 400, 0, 518, mPaint);//
//
//
//        canvas.rotate(mGradee * 2);
//        canvas.drawLine(0, 400, 0, 518, mPaint);//
//        canvas.rotate(mGradee);
//        canvas.drawLine(0, 400, 0, 512, mPaint);
//        canvas.rotate(mGradee);
//        canvas.drawLine(0, 400, 0, 506, mPaint);//


//        canvas.drawLine(0, 400, 0, 500, mPaint);


    }
}
